#!/usr/bin/env python3

import os
import csv
import urllib.request
import base64

serversnum = 30
profiles = []
choice = -1
mirrorlist = [ "https://www.vpngate.net", "http://5.181.235.14:29916", "http://88.218.17.24:20261", "http://138.199.46.86:36667", "http://109.111.243.206:17579", "http://78.142.193.246:33304", "http://163.182.174.159:18358", "http://91.193.75.5:45082", "http://103.201.129.226:14684", "http://146.70.205.2:6283", "http://146.70.205.6:34299", "http://62.133.35.246:2265" ]
reqpart = "/api/iphone/"
# listurl = "http://www.vpngate.net/api/iphone/"
ovpnpath = "/tmp/profile.ovpn"
fmtstrhead = "{:^4} | {:^4} | {:^4} | {:^30} | {:^10}"
fmtstr     = "[{:>2}] |  {}  | {:>4} | {:<30} | {:^10}"

## Mirror selection
mirchoice = -1
fmtmirr   = "[{:3}] | {:>40}"
for i in range(len(mirrorlist)):
    print(fmtmirr.format( i, mirrorlist[i] ))

while mirchoice < 0 or mirchoice > (len(mirrorlist) - 1):
    mirchoice = int(input("Select a mirror:: "))

listurl = mirrorlist[mirchoice] + reqpart
response = urllib.request.urlopen(listurl).read().decode('utf-8').split('\n')
reader = csv.reader(response, delimiter=',')

# Get rid of irrelevant headers
reader.__next__()
reader.__next__()

print(fmtstrhead.format( "INDX", "LCTN", "Ping", "Server ID", "Score" ))
for i in range(serversnum):
    current = reader.__next__()
    print(fmtstr.format( i, current[6], current[3], current[0], current[2] ))
    profiles.append(current[14])

while choice < 0 or choice > serversnum:
    choice = int(input("Choose a profile: "))

scriptdir = os.path.expanduser("~/.config/openvpn")
with open(ovpnpath, 'w') as f:
    f.write(base64.b64decode(profiles[choice]).decode('ascii'))
    f.write( "script-security 2\n" )
    f.write( "up \"" + scriptdir + "/up.sh\"\n" )
    f.write( "down \"" + scriptdir + "/down.sh\"\n" )
    f.write( "data-ciphers 'AES-128-CBC:AES-256-GCM:AES-128-GCM:CHACHA20-POLY1305'\n" )

# os.system( "sudo openvpn " + runopts + "--config" + ovpnpath )
os.system( "sudo openvpn " + ovpnpath )
